<template>
  <el-dialog title="添加页面" :visible="showDialog" @close="closeFn">
    <el-form ref="form" label-width="70px" :model="formData" :rules="rules">
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <quillEditor v-model="formData.content" @blur="$refs.form.validateField('content')" />
      </el-form-item>
    </el-form>
    <el-row type="flex" justify="end">
      <el-button type="primary" size="small" @click="submit">确认</el-button>
      <el-button size="small" @click="closeFn">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import { addPages, getPagesDetail, editPages } from '@/api/pages'
export default {
  components: {
    quillEditor
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '', // 标题
        content: ''// 内容
      },
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ]
      }

    }
  },
  methods: {
    // 关闭弹窗
    closeFn() {
      this.$emit('update:showDialog', false)// 关闭弹窗
      this.formData = {
        title: '', // 标题
        content: ''// 内容
      }
      // 关闭错误校验
      this.$refs.form.resetFields()
    },
    // 增添
    async submit(id) {
      await this.$refs.form.validate()// 表单验证
      if (this.formData.id) {
        // 编辑数据
        await editPages(this.formData)
      } else {
        // 新增数据
        await addPages(this.formData)
      }
      this.closeFn() // 关闭弹窗
      // 消息提示
      this.$message({
        type: 'success',
        message: '操作成功!'
      })
      this.$emit('updateEvent') // 刷新列表
    },
    // 获取页面详情
    async  getPagesDetail(id) {
      const res = await getPagesDetail(id)
      // 回显
      //   console.log(99, res)
      this.formData = res.data
    }
  }
}
</script>

<style>

</style>
